
package es.ceu.mp2.conway;

public class GameBoard {
    private boolean[][] board;

    public GameBoard(int row,int column){
        this.board = new boolean[row][column];
    }

    public GameBoard(boolean[][] board){
        this.board = board;
    }
    
    public void setCellAlive(int row, int column){
        board[row][column] = true;
    }

    public void setCellDeath(int row, int column){
        board[row][column] = false;
    }

    public int getRows(){
        return board.length;
    }

    public int getColumns(){
        return board[0].length;
    }

    public boolean isAlive(int row, int column){
        return board[row][column];
    }

    public boolean printTable(ConwayGUI gameGUI){
        return gameGUI.printBoard(this);
    }

    public void nextIteration(){
        int totalRows = this.getRows();
        int totalColumns = this.getColumns();
        int neighbours = 0;
        boolean[][] nextBoard = new boolean[totalRows][totalColumns];

        for (int row = 0; row < totalRows; row++)
            for (int column = 0; column < totalColumns; column++){
                neighbours = getNeighboursAlive(row, column);
                if(wouldItLive(this.isAlive(row, column), neighbours))
                    nextBoard[row][column] = true;
                else
                    nextBoard[row][column] = false;
            }

        this.board = nextBoard;
    }

    private int getNeighboursAlive (int row, int column){
        int neighboursAlive = 0;

        if(isNeighbourAlive(row-1, column-1)) neighboursAlive++;
        if(isNeighbourAlive(row-1, column)) neighboursAlive++;
        if(isNeighbourAlive(row-1, column+1)) neighboursAlive++;
        if(isNeighbourAlive(row, column-1)) neighboursAlive++;
        if(isNeighbourAlive(row, column+1)) neighboursAlive++;
        if(isNeighbourAlive(row+1, column-1)) neighboursAlive++;
        if(isNeighbourAlive(row+1, column)) neighboursAlive++;
        if(isNeighbourAlive(row+1, column+1)) neighboursAlive++;

        return neighboursAlive;
    }

    private boolean isNeighbourAlive(int row, int column) {
        boolean alive = false;

        if (row == this.getRows())
            row = 0;
        else if (row == -1)
            row = this.getRows() - 1;
        if (column == this.getColumns())
            column = 0;
        else if (column == -1)
            column = this.getColumns() -1;

        alive = this.isAlive(row, column);
        
        return alive;
    }

    private boolean wouldItLive(boolean isAlive, int neighbours){
        if (isAlive){
            if (neighbours < 2 || neighbours > 3)
                return false;
            else
                return true;
        }
        else if(neighbours == 3)
            return true;

        return false;
    }

    @Override
    public boolean equals(Object obj){
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;

        final GameBoard other = (GameBoard) obj;

        int row = this.getRows();
        if (row != other.getRows())
            return false;

        int column = this.getColumns();
        if (column != other.getColumns())
            return false;

        for (int i = 0; i < row; i++)
            for (int j = 0; j < column; j++){
                if (this.isAlive(i, j) != other.isAlive(i, j))
                    return false;
            }
        return true;

    }

}
